package com.bincloud.vue.admin.web.controller.ums;


import com.alibaba.fastjson.JSON;
import com.bincloud.vue.admin.common.model.dto.response.BasePageResDTO;
import com.bincloud.vue.admin.common.model.dto.response.BaseResDTO;
import com.bincloud.vue.admin.common.util.ResponseUtils;
import com.bincloud.vue.admin.web.dto.ums.role.*;
import com.bincloud.vue.admin.web.service.ums.IUmsRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bincloud, mvpzhou
 * @since 2022-02-17
 */
@Api(tags = "角色相关接口")
@RestController
@RequestMapping("/admin/ums/role")
@Slf4j
public class UmsRoleController {
    @Resource
    private IUmsRoleService umsRoleService;

    @PreAuthorize(value = "hasAuthority('admin:ums:role')")
    @ApiOperation(value = "角色分页查询", produces = "application/json")
    @PostMapping(value = "/page")
    public BaseResDTO<BasePageResDTO<List<UmsRolePageResDTO>>> page(@Validated @RequestBody UmsRolePageReqDTO reqDTO) {
        log.info("角色分页查询,入参:{}", JSON.toJSONString(reqDTO));
        BasePageResDTO<List<UmsRolePageResDTO>> resDTO = umsRoleService.pageRole(reqDTO);
        return ResponseUtils.ok(resDTO);
    }

    @PreAuthorize(value = "hasAuthority('admin:ums:role')")
    @ApiOperation(value = "角色新增", produces = "application/json")
    @PostMapping(value = "/save")
    public BaseResDTO<Boolean> save(@Validated @RequestBody UmsRoleSaveReqDTO reqDTO) {
        log.info("角色新增,入参:{}", JSON.toJSONString(reqDTO));
        boolean resDTO = umsRoleService.saveRole(reqDTO);
        log.info("角色新增,返参:{}", JSON.toJSONString(resDTO));
        return ResponseUtils.ok();
    }

    @PreAuthorize(value = "hasAuthority('admin:ums:role')")
    @ApiOperation(value = "角色删除", produces = "application/json")
    @PostMapping(value = "/remove")
    public BaseResDTO<Boolean> remove(@Validated @RequestBody UmsRoleReqDTO reqDTO) {
        log.info("角色删除,入参:{}", JSON.toJSONString(reqDTO));
        boolean resDTO = umsRoleService.removeRole(reqDTO);
        log.info("角色删除,返参:{}", JSON.toJSONString(resDTO));
        return ResponseUtils.ok();
    }

    @PreAuthorize(value = "hasAuthority('admin:ums:role')")
    @ApiOperation(value = "角色更新", produces = "application/json")
    @PostMapping(value = "/modify")
    public BaseResDTO<Boolean> modify(@Validated @RequestBody UmsRoleModifyReqDTO reqDTO) {
        log.info("角色更新,入参:{}", JSON.toJSONString(reqDTO));
        boolean resDTO = umsRoleService.modifyRole(reqDTO);
        log.info("角色更新,返参:{}", JSON.toJSONString(resDTO));
        return ResponseUtils.ok();
    }

}

